package com.main.ok.lucene.mysql;

import java.io.IOException;
import java.nio.file.Paths;
import java.text.ParseException;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.LongField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.springframework.jdbc.core.JdbcTemplate;

import com.google.common.collect.Lists;
import com.main.ok.common.DateUtils;
import com.main.ok.common.RunSqlTool;

public class BuildIndexFromMysql {
    private static final String indexPath = "g:/lucene";

    public static void main(String[] args) throws ParseException, IOException {
        int k = 0;
        while (k < 1000000) {
            // build data
//            List<MsgHisDto> mList = buildSqlData(k, k + 20000);
            // build index
//            buildIndexes(mList);
            System.out.println(k+"-"+(k+20000));
            k += 20001;
            System.out.println(k);
        }
    }

    private static void buildIndexes(List<MsgHisDto> mList) throws IOException {
        Directory d = FSDirectory.open(Paths.get(indexPath));
        Analyzer ana = new StandardAnalyzer();
        IndexWriterConfig config = new IndexWriterConfig(ana);
        config.setOpenMode(OpenMode.CREATE);

        IndexWriter writer = new IndexWriter(d, config);

        for (MsgHisDto msghis : mList) {
            Document document = new Document();
            document.add(new LongField("id", msghis.getId(), Field.Store.YES));
            document.add(new StringField("memo", msghis.getMemo(), Field.Store.YES));
            document.add(new StringField("status", msghis.getStatus().toString(), Field.Store.YES));
            document.add(new StringField("gmt_created", DateUtils.transferD2String(msghis.getGmtCreated()), Field.Store.YES));
            document.add(new TextField("content", msghis.getContent(), Field.Store.YES));
            writer.addDocument(document);
            System.out.println("msgHis:" + msghis.getId() + " add to writer");
        }

        writer.close();
        System.err.println("everytring has been indexed...");
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    private static List<MsgHisDto> buildSqlData(int start, int end) throws ParseException {
        JdbcTemplate template = RunSqlTool.getJdbcTemplate();
        String sql = "select * from msg_his limit " + start + "," + end;
        List result = template.queryForList(sql);
        List<MsgHisDto> resultList = Lists.newArrayList();
        for (int i = 0; i < result.size(); i++) {
            LinkedHashMap<String, Object> map = (LinkedHashMap<String, Object>) result.get(i);
            MsgHisDto dto = new MsgHisDto();
            dto.setContent(String.valueOf(map.get("content")));
            dto.setMemo(String.valueOf(map.get("memo")));
            dto.setGmtCreated((Date) map.get("gmt_created"));
            dto.setId((long) (map.get("id")));
            dto.setStatus(Integer.parseInt(map.get("status").toString()));
            resultList.add(dto);
        }
        return resultList;
    }
}
